<?php
/**
 * Created by PhpStorm.
 * Author: huangwy
 * Date: 2018/3/16 9:29
 * Version: 1.0
 */

namespace app\admin\controller;

use function MongoDB\BSON\fromJSON;
use think\Db;

class Menu extends Base
{
    public function index()
    {
        $prefix = config('database.prefix');

        $list = Db::query("select * from {$prefix}auth_rule order by o asc");
        $list = getMenu($list);

        $this->assign('list', $list);
        return view();
    }

    public function del()
    {
        $ids = input('ids');
        //判断子类

        $del = model('Menu')->menu_del($ids);

        if ($del) {
            if($del === 999){
                return json(['flag'=>1,'message'=>'请勾选删除菜单!']);
            }elseif($del === 998){
                return json(['flag'=>1,'message'=>'有子栏目，不允许删除！']);
            }else{
                return json(['flag'=>0,'message'=>'恭喜，菜单删除成功！']);
            }

        } else {
            return json(['flag'=>1,'message'=>'删除失败！']);
        }
    }

    public function edit($id = 0)
    {
        $id = intval($id);
        $m = db('auth_rule');
        $currentmenu = $m->where("id='$id'")->find();
        if (!$currentmenu) {
            $this->error('参数错误！');
        }

        $option = model('Menu')->menu();

        $this->assign('option', $option);
        $this->assign('currentmenu', $currentmenu);
        return view('form');
    }


    public function add()
    {
        $option = model('Menu')->menu();

        $this->assign('option', $option);
        return view('form');
    }

    public function update()
    {
        $info = input();
        $update = model('Menu')->menu_update($info);
        if ($update) {
            return json(['flag'=>0,'message'=>'操作成功！']);
        }else{
            return json(['flag'=>1,'message'=>'操作失败！']);
        }
    }
}